<?php
class AddressesController extends AppController {
    var $name = 'Addresses';
    

    function index() {
        $this->Address->recursive = 0;
        $this->set('addresses', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid address', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('address', $this->Address->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Address->create();
            if ($this->Address->save($this->data)) {
                $this->Session->setFlash(__('The address has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The address could not be saved. Please, try again.', true));
            }
        }
        $addressProvinces = $this->Address->AddressProvince->find('list',
                        array('conditions' => array('AddressProvince.territory_type' => 'PROVINCE')));

		// Inicialmente vacio, se setea cuando elige un addressProvince                        
        $addressCities = array();
		// Inicialmente vacio, se setea cuando elige un addressCity
        $addressNeighborhoods = array();
        $this->set(compact('addressProvinces', 'addressCities', 'addressNeighborhoods'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid address', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Address->save($this->data)) {
                $this->Session->setFlash(__('The address has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The address could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Address->read(null, $id);
        }
        $addressProvinces = $this->Address->AddressProvince->find('list',
                        array('conditions' => array('AddressProvince.territory_type' => 'PROVINCE')));
        $addressCities = $this->Address->AddressCity->find('list',
                        array('conditions' => array('AddressCity.territory_type' => 'CITY')));
        $addressNeighborhoods = $this->Address->AddressNeighborhood->find('list',
                        array('conditions' => array('AddressNeighborhood.territory_type' => 'NEIGHBORHOOD')));
        $this->set(compact('addressProvinces', 'addressCities', 'addressNeighborhoods'));
    }
    
	function update_select(){

	    if(!empty($this->data['Address']['province_id'])) {              
	        $addressProvince = $this->data['Address']['province_id'];
	
	        $options = $this->Address->AddressCity->find('list',
	        array('conditions' => array('AddressCity.parent_territory_id' => $addressProvince)));
	        
	      }
	      
		if(!empty($this->data['Address']['city_id'])) {              
	        $addressCity = $this->data['Address']['city_id'];
	
	        $options = $this->Address->AddressNeighborhood->find('list',
	        array('conditions' => array('AddressNeighborhood.parent_territory_id' => $addressCity)));
	        
	      }
	      
    	$this->set(compact('options'));       
	}
    
}
?>
